package com.jeremyfeinstein.okhttputil.callback;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.jeremyfeinstein.gson.EncryptResponseBean;
import com.jeremyfeinstein.utils.AESUtil;

import okhttp3.Response;

import java.io.IOException;

/**
 * Created by zhy on 15/12/14.
 */
public abstract class StringCallback extends Callback<String> {

    private boolean isAESDecode = true;

    public StringCallback() {
        this.isAESDecode = true;
    }

    public StringCallback(boolean isAESDecode) {
        this.isAESDecode = isAESDecode;
    }

    private static final String TAG = StringCallback.class.getSimpleName();

    @Override
    public String parseNetworkResponse(Response response) throws IOException {
        String res = response.body().string();
        String result;
        if (isAESDecode) {
            if (!TextUtils.isEmpty(res)) {
                EncryptResponseBean bean = new Gson().fromJson(res, EncryptResponseBean.class);
                result = AESUtil.decrypt(bean.getValue());
            } else {
                result = res;
            }
        } else {
            result = res;
        }
        Log.d(TAG, "返回数据\n========>\n" + result + "\n<========");
        return TextUtils.isEmpty(res) ? null : result;
    }

}
